<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
    :visible="showAssignDialog"
    @close="close"
  >
    <!-- 权限点数据展示 -->
    <el-tree
      ref="tree"
      :data="list"
      node-key="id"
      :props="{ label: 'name' }"
      show-checkbox
      check-strictly
    ></el-tree>

    <template #footer>
      <el-button @click="$emit('update:showAssignDialog', false)">
        取消
      </el-button>
      <el-button type="primary" @click="assignPerm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { transformTreeData } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/setting'

/**
 * 1.新建组件,注册,使用
 * 2.获取权限数据,渲染
 * 3.操作数据
 */
export default {
  props: {
    showAssignDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      list: [], // 所有权限列表
      emplId: '' // 当前点击员工的id
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // -----------------------------关闭弹层执行----------------------------
    close () {
      this.$emit('update:showAssignDialog', false)
      this.$refs.tree.setCheckedKeys([])
    },
    // -----------------------------获取权限列表------------------------------
    async getList () {
      const list = await getPermissionList()
      // console.log(list)
      this.list = transformTreeData(list)
    },
    // --------------------------------数据回显--------------------------------
    async getRoleDetail (id) {
      this.emplId = id
      const { permIds } = await getRoleDetail(id)
      // console.log(permIds)
      this.$refs.tree.setCheckedKeys(permIds)
    },
    // 修改权限
    async assignPerm () {
      // {id:角色id, permIds:[] 所有选中的节点的id组成的数组}
      await assignPerm({ id: this.emplId, permIds: this.$refs.tree.getCheckedKeys() })
      this.$emit('update:showAssignDialog', false)
      this.$message.success('修改权限成功')
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
